<?php
//require_once(__DIR__.'/../config/authentication.php');

class PreSystemHook 
{

    
    function doAuthorize()
    {
        $_SERVER['PATH_INFO'] = isset($_SERVER['QUERY_STRING']) ? strtolower($_SERVER['QUERY_STRING']) : strtolower($_SERVER['PATH_INFO']);
		// echo $_SERVER['PATH_INFO']; exit;
       // var_dump($_SERVER);
        
        //session_start();
        /*
        if ($this->is_public_url() === FALSE)
        {
            // check authorization, if not then redirect to authentication page
            if ($this->is_authorize() === FALSE)
            {
                global $authentication_url;
                header('Location: '.$authentication_url);
                exit;
            }
        }
        else 
        {
            // unset replay condition for public pages.
            if(strpos($_SERVER['PATH_INFO'], '/login_controller') === FALSE)
            {
                $_SESSION['replay'] = FALSE;
            }
        }
        */
    }

    /*
    * check user has permission on requested URL.
    */
    function is_authorize() {
        
        // check user
        global $url_permission;
        
        // step 1. check user is authenticated
        if (! isset($_SESSION['user'])) 
        {
            $regular_expression = "/^\/[a-z_]+_controller\//";
            if(preg_match ($regular_expression,$_SERVER['PATH_INFO']))
            {
                // log requested url and data 
                $_SESSION['replay'] = TRUE;
                $_SERVER['PATH_INFO'] = '/general_controller/save_unauthenticate_request';
            }
            else
            {
                header("HTTP/1.0 404 Not Found");
                exit;
            }
            
            return TRUE;
            
        }
        
        // step 2. check user has permission on requested url
        // if  url permission is not set then assume its restricted.
        $request_uri = $_SERVER['PATH_INFO'];
        $position = strpos($request_uri, '/', 1);
        $position = ($position !== FALSE ) ? strpos($request_uri, '/', $position+1) : FALSE;
        if ($position !== FALSE) {
            $request_uri = substr($request_uri,0, $position);
        }
        
        if(! isset($url_permission[$request_uri])) 
        {
            global $access_denied_url;
            header('Location: '.$access_denied_url);
            exit;
        }

        // step 3. check user has permission on requested URL.        
        $user = $_SESSION['user'];
        $permission = $url_permission[$request_uri];

        if (($user['user_type_id'] & $permission[0]) > 0  && ($user['user_role_id'] & $permission[2]) > 0) 
        {
            return TRUE;
        }
        else 
        {
            // not authorize to access requested url, redirect to access denied page
            global $access_denied_url;
            header('Location: '.$access_denied_url);
            exit;
        }
        
        return FALSE;
        
    }
    
    /*
    * check requested URL is public 
    */
    function is_public_url() {
        
        global $public_url;
        
        $request_uri = $_SERVER['PATH_INFO'];
        
        foreach ($public_url as $url) 
        {
            if(strpos($request_uri, $url) !== FALSE)
                return TRUE;
        }

        return FALSE;
    }
}

/* End of file pre_system_hook.php */
/* Location: ./system/application/hooks/pre_system_hook.php */
